﻿using System;
using System.ServiceModel.DomainServices.Client;
using System.Windows.Data;
using GSSoftSL.Utility;
using GSSoftSL.Web.Models;
using GSSoftSL.Web.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Windows.Data.DomainServices;

namespace GSSoftSL.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class PhongViewModel : ViewModelBase
    {
        private bool _isBusy = false;
        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                RaisePropertyChanged("IsBusy");
            }
        }

        public int[] Tangs
        {
            get { return new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; }
        }

        private int _value;

        public int CurrentInt
        {
            get { return _value; }
            set
            {
                _value = value;
                RaisePropertyChanged("CurrentInt");
            }
        }

        private GSSoftContext _dc;

        /// <summary>
        /// Initializes a new instance of the PhongViewModel class.
        /// </summary>
        public PhongViewModel()
        {
            Init();
            //SaveCommand = new RelayCommand<object>(SaveChanges, (o) => !_dc.IsSubmitting);
            //CurrentInt = 3;
        }

        private void Init()
        {
            _dc = _dc = new GSSoftContext();//(GSSoftContext)context;
            IsBusy = true;
            _dc.Load<Phong>(_dc.GetPhongsQuery(), LoadBehavior.RefreshCurrent, LoadPhong_Completed, null);
        }

        private void LoadPhong_Completed(LoadOperation<Phong> load)
        {
            Phongs = new PagedCollectionView(new EntityList<Phong>(_dc.Phongs, load.Entities));
            IsBusy = false;
        }

        private void Complete(object sender, EventArgs e)
        {
            IsBusy = false;
        }

        
        private PagedCollectionView _phongs = null;
        public PagedCollectionView Phongs
        {
            get
            {
                return _phongs;
            }
            set
            {
                if (_phongs == value)
                {
                    return;
                }
                _phongs = value;
                RaisePropertyChanged("Phongs");
            }
        }

        
        private Phong _current = null;
        public Phong Current
        {
            get
            {
                return _current;
            }
            set
            {
                if (_current == value)
                {
                    return;
                }
                _current = value;
                if (_current != null && (_current.EntityState == EntityState.New || _current.MaPhong == null))
                    _current.MaPhong = "";
                RaisePropertyChanged("Current");
            }
        }

        private RelayCommand<object> _saveCommand;
        public RelayCommand<object> SaveCommand
        {
            get
            {
                return _saveCommand
                    ?? (_saveCommand = new RelayCommand<object>(
                                          o =>
                                          {
                                              if (Current == null || !_dc.HasChanges || _dc.IsSubmitting)
                                                  return;
                                              ConfirmWindow.CreateNew("Bạn có muốn lưu?", Save);
                                          },
                                          o => !(Current == null || !_dc.HasChanges || _dc.IsSubmitting)));
            }
        }

        private void Save()
        {
            if (Current.EntityState == EntityState.New)
            {
                Current.NgayTao = Current.NgayCapNhat = DateTime.Now;
                Current.NguoiTao = Current.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
            }
            else if (Current.EntityState == EntityState.Modified)
            {
                Current.NgayCapNhat = DateTime.Now;
                Current.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
            }
            IsBusy = true;
            _dc.SubmitChanges(SubmitChangesEnd, null);
        }

        /*private void SaveChanges(object obj)
        {
            if (Current == null || !_dc.HasChanges)
                return;
            if (Current.EntityState == EntityState.New)
            {
                Current.NgayTao = Current.NgayCapNhat = DateTime.Now;
                Current.NguoiTao = Current.NguoiCapNhat = new Guid("48F27C56-0DC4-4609-A6B0-B9BA83A7FAAF");
            }
            else if (Current.EntityState == EntityState.Modified)
            {
                Current.NgayCapNhat = DateTime.Now;
                Current.NguoiCapNhat = new Guid("48F27C56-0DC4-4609-A6B0-B9BA83A7FAAF");
            }
            if (!_dc.IsSubmitting)
            {
                IsBusy = true;
                _dc.SubmitChanges(SubmitChangesEnd, null);
            }
        }*/

        private void SubmitChangesEnd(SubmitOperation obj)
        {
            IsBusy = false;
        }

        private RelayCommand _refreshCommand;
        public RelayCommand RefreshCommand
        {
            get
            {
                return _refreshCommand
                    ?? (_refreshCommand = new RelayCommand(Init));
            }
        }
    }
}